import { _decorator, Component, Node, tween, v3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GameEndWinAni')
export class GameEndWinAni extends Component {
    //喇叭的节点
    @property({ type: [Node] })
    public nodes: Node[] = [];

    @property({ type: [Node] })
    public starts: Node[] = [];

    @property({ type: Node })
    public hg: Node = null;

    protected onLoad(): void {
        //喇叭的动作
        let v3_pos = [];
        for (let i = 0; i < this.nodes.length; i++) {
            v3_pos.push(this.nodes[i].getPosition());
        }
       
        for (let i = 0; i < this.nodes.length; i++) {
            let node = this.nodes[i];
            if(i >= 2){
                tween()
                .target(node)
                .hide()
                .to(0, { position: v3(0, 0, 0) })
                .show()
                .to(0.5, { position: v3_pos[i] }, { easing: 'backIn' })
                .then(
                    tween()
                        .to(0.5, { scale: v3(-1.1, 1.1, 1.1) })
                        .to(0.5, { scale: v3(-0.8, 0.8, 0.8) })
                )
                .repeat(999999)
                .start()
            }else{
                tween()
                .target(node)
                .hide()
                .to(0, { position: v3(0, 0, 0) })
                .show()
                .to(0.5, { position: v3_pos[i] }, { easing: 'backIn' })
                .then(
                    tween()
                        .to(0.5, { scale: v3(1.1, 1.1, 1.1) })
                        .to(0.5, { scale: v3(0.8, 0.8, 0.8) })
                )
                .repeat(999999)
                .start()
            }
        }

        //星星的动作
        let v3_pos1 = [];
        for (let i = 0; i < this.starts.length; i++) {
            v3_pos1.push(this.starts[i].getPosition());
        }
        let times = [0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95];
        let idx = Math.floor(Math.random()*times.length);
        for (let i = 0; i < this.starts.length; i++) {
            let node = this.starts[i];
            tween()
                .target(node)
                .hide()
                .to(0, { position: v3(0, 0, 0) })
                .show()
                .to(times[idx], { position: v3_pos1[i] }, { easing: 'backIn' })
    
            .then(
                tween()
                .to(times[idx], { scale: v3(0.5, 0.5, 0.5) })
                .to(times[idx], { scale: v3(1, 1, 1) })
            )
            .repeat(999999)
            .start()            
        }

        //皇冠
        tween()
            .target(this.hg)
            .hide()
            .to(0, { position: v3(0, 0, 0) })
            .show()
            .to(0.5, { position: v3(0,112.941,0)}, { easing: 'backIn' })
            .start()
    }

    start() {

    }

    update(deltaTime: number) {
        
    }
}


